#!/usr/bin/awk -f

BEGIN {
    Time_begin = systime();
    print strftime("%Y-%m-%d %T",Time_begin) " Starting...";
}

{
    if(NR % 10000000 == 0) {
        print NR;
    }

    if(NF != 10){
        Str="";
        # " 切分数组长度为10，替换其中的空格为%20
        split($0,Arr,/\042/);
        Arr_len = length(Arr);
        if(Arr_len != 10){
            for(i=1;i<=Arr_len;i++){
                if(i == 2){
                    Arr[i] = "\""Arr[i]"\"";
                }else if(i == 4 || i == 6 || i == 8 || i == 10){
                    gsub(/ /,"",Arr[i]);
                    Arr[i] = "\""Arr[i]"\"";
                }
                Str = Str""Arr[i];
            }
            $0 = Str;
        }
    }

    gsub(/\042|\047/,"",$4);
    response_time = $9;
    match_url = "";
    query_string = ($5 ~ /-/) ? $6 : $5;
    gsub(/\"/,"",query_string);

    ###2.0接口匹配规则，不循环数组
    if(match($4,/(openapi-wireless)?\/((get|add|send|searchV|searchS|login|verifyUser|doRegister|hellotest|test)[a-zA-Z\/]+)/,old) != 0){
        match_url = old[2];
        parse_query(query_string,result_1);
        pid = result_1["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(openapi-wireless)?\/(videos)(\/(X[^/]+?|[0-9]+?))(\/[a-z\/]+)\.?(json|xml|text)?$/,news_1) != 0){
        match_url = news_1[2]news_1[5];
        parse_query(query_string,result_2);
        pid = result_2["pid"];
        interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(openapi-wireless)?\/(videos\/search|shows\/search|ctrl\/clear_cache).*?$/,news_4) != 0){
        match_url = news_4[2];
        parse_query(query_string,result_3);
        pid = result_3["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(openapi-wireless)?\/(shows|albums|layout\/apple|internal\/cms\/phone\/channels|internal\/cms\/pad\/channels|internal\/cms\/channels)(\/[^/]+?)(\/[a-z\/]+)\.?(json|xml|text)?$/,news_2) != 0){
        match_url = news_2[2]news_2[4];
        parse_query(query_string,result_4);
        pid = result_4["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(openapi-wireless)?\/(user\/)(.*?)\/?(uploads)\.?(json|xml|text)?$/,news_3) != 0){
        match_url = news_3[2]news_3[4];
        parse_query(query_string,result_5);
        pid = result_5["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(openapi-wireless)?\/(videos\/playurl)$/,news_11) != 0) {
        match_url = news_11[2];
        parse_query(query_string,result_6);
        pid = result_6["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(openapi-wireless)?\/(shows|videos|layout\/phone2\/related)(\/[0-9a-zA-Z\/]+)\.?(json|xml|text)?$/,news_22) != 0){
        match_url = news_22[2];
        parse_query(query_string,result_7);
        pid = result_7["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }else if(match($4,/(open-wireless)?\/([a-zA-Z0-9\/]+)\.?(json|xml|text)?$/,news_5)){
        match_url = news_5[2];
        parse_query(query_string,result_8);
        pid = result_8["pid"];
        # interface_stat(match_url,response_time);
        interface_count(match_url,response_time,pid);
    }
}

END {
    # for(iface in all_request_times) {
    for(key in all_request_times) {
        iface = key;

        #接口平均时长s
        avage = all_time_len[iface] / all_request_times[iface];

        #接口异常数量
        exception = (all_time_exception[iface] / all_request_times[iface]) * 100;

        print iface,"\t",all_request_times[iface],"\t",avage,"\t",exception >> "iface_stat";
    }

    for(m in all_request_times_pid) {
        split(m,tmp_all_result_times,SUBSEP);
        iface_pid = tmp_all_result_times[1];
        pid = tmp_all_result_times[2];

        #接口平均时长s
        avage_pid = all_time_len_pid[iface_pid,pid] / all_request_times_pid[m];

        #接口异常数量
        exception_pid = (all_time_exception_pid[iface_pid,pid] / all_request_times_pid[m]) * 100;

        #接口调用次数与PID
        print iface_pid,"\t",pid,"\t",all_request_times_pid[m],"\t",avage_pid,"\t",exception_pid >> "iface_count";
    }

    if(Time_begin){
        Time_end = systime();
        print strftime("%Y-%m-%d %T",Time_end) " End";
        print "Take Time: " Time_end - Time_begin "s";
    }
}

#interface stat
function interface_stat(_url,_response_time) {
    all_request_times[_url] += 1;
    all_time_len[_url] += _response_time;
    if(_response_time > 0.2) {
        all_time_exception[_url] += 1;
    }
}

#interface count
function interface_count(_url,_response_time,_pid) {
    all_request_times_pid[_url,_pid] += 1;
    all_time_len_pid[_url,_pid] += _response_time;
    if(_response_time > 0.2) {
        all_time_exception_pid[_url,_pid] += 1;
    }
}

#parse_query
function parse_query(str,array) {
    delete array;
    split(str,arr,"&");
    for(i=1;i<=length(arr);i++) {
        split(arr[i],tmp,"=");
        if(length(tmp) > 2){
	    tmp_offset = index(arr[i],"=");
	    array[tmp[1]] = substr(arr[i],tmp_offset,length(arr[i]) - length(tmp[1]));
        }else{
            array[tmp[1]] = tmp[2];
        }
    }
}
